package org.zht.dormpro.dorm.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zht.dormpro.auth.bean.PageBean;
import org.zht.dormpro.dorm.bean.ClassDTO;
import org.zht.dormpro.dorm.model.TbClass;
import org.zht.dormpro.dorm.model.TbCollege;
import org.zht.dormpro.dorm.model.TbMajor;
import org.zht.dormpro.dorm.service.IClassService;
import org.zht.dormpro.dorm.service.ICollegeService;
import org.zht.dormpro.dorm.service.IMajorService;
import org.zht.dormpro.pub.ResponseMap;

import java.util.*;

@Controller
@RequestMapping("/dorm/class")
public class ClassController {

    @Autowired
    public IClassService classService;
    @Autowired
    public ICollegeService collegeService;
    @Autowired
    public IMajorService majorService;

    /**
     * 跳转到班级页面
     * @param model
     * @return
     */
    @RequestMapping("/classPage")
    public String classPage(Model model){
        List<TbCollege> colleges = collegeService.getAllCollege(0);
        model.addAttribute("colleges",colleges);

        return "/class/class_list";
    }

    /**
     * 异步请求通过院校级联专业
     * @param colId
     * @return
     */
    @RequestMapping("/listMajor")
    @ResponseBody
    public ResponseMap listMajor(@RequestParam("id") String colId){
        //TODO 像这样的都是可以抽出一个公共controller.
        List<TbMajor> majors = majorService.getMajorByCollegeId(colId);
        return ResponseMap.success(majors);
    }

    /**
     * 班级页面
     * @param params
     * @return
     */
    @RequestMapping("/listClass")
    @ResponseBody
    @RequiresPermissions("dorm:list:all")
    public ResponseMap listClass(@RequestParam Map<String,Object> params){
        PageBean pageBean = new PageBean(params);
        List<ClassDTO> classes = classService.getClassByCondition(pageBean);
        Map<String,Object> attr = new HashMap<>();
        attr.put("total",pageBean.getTotal());
        return ResponseMap.success(classes,attr);
    }

    /**
     * 跳转到添加页面
     * @param model
     * @return
     */
    @RequestMapping("/addClassPage")
    public String addClassPage(Model model){
        List<TbMajor> majors = majorService.getAllMajor(0);
        model.addAttribute("majors",majors);
        return "/class/class_add";
    }

    /**
     * 添加班级信息
     * @param tbClass
     * @return
     */
    @RequestMapping(value = "/addClass",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap addClass(TbClass tbClass){
        boolean success = classService.addClass(tbClass);
        if(success){
            return ResponseMap.success("专业添加成功");
        }else {
            return ResponseMap.error("专业添加失败");
        }
    }

    /**
     * 跳转到编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/classEditPage/{id}")
    public String classEditPage(Model model, @PathVariable("id") Integer id){
        TbClass tbClass = classService.selectByPrimaryKey(id);
        List<TbCollege> colleges = collegeService.getAllCollege(0);
        List<TbMajor> majors = majorService.getAllMajor(0);
        model.addAttribute("class",tbClass);
        model.addAttribute("colleges",colleges);
        model.addAttribute("majors",majors);

        return "/class/class_edit";
    }

    /**
     * 修改班级信息
     * @param tbClass
     * @return
     */
    @RequestMapping(value = "/editClass",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap editClass(TbClass tbClass){
        boolean success = classService.editClass(tbClass);
        if(success){
            return ResponseMap.success("班级修改成功");
        }else {
            return ResponseMap.error("班级修改失败");
        }
    }

    /**
     * 删除班级信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteClass",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap deleteClass(Integer id){
        boolean success = classService.deleteClass(id);
        if (success){
            return ResponseMap.success("班级删除成功");
        }else {
            return ResponseMap.error("班级删除失败");
        }
    }

    /**
     * 批量删除
     * @param claIds
     * @return
     */
    @RequestMapping(value = "/batchRemoveClass",method = RequestMethod.POST)
    @ResponseBody
    public ResponseMap batchRemoveClass(@RequestParam("claIds[]") String[] claIds){
        boolean success = classService.batchRemoveClass(new ArrayList<String>(Arrays.asList(claIds)));
        if (success){
            return ResponseMap.success("批量删除班级成功");
        }else {
            return ResponseMap.error("批量删除班级失败");
        }
    }
}
